define(["angularAMD", "module/system/service/LogService"], function (angularAMD) {
    angularAMD.service('LoggingService', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog', '$calculater', '$LogService',
        function($rootScope, $filter, $translate, config, $MMWService, $mdDialog, $calculater, $LogService){
            this.init = function(options){
                var recordList = [];
                var log_path = '';
                var total_size = 0;

                //檢查目前平台是否支援
                function deviceCheck(callback){
                    if(window.nodeRequire){
                        if(callback)
                            callback();
                    }else{
                        //此功能僅支援桌面版本
                        $rootScope.showAlert($filter('translate')('logging.msg.device_not_support'));
                    }
                }

                //取得開窗所需資訊
                function getLogInformation(callback){
                    getLogPath(function(){
                        getLogList(function(){
                            if(callback)
                                callback();
                        });
                    });
                }

                //取得Log檔案路徑
                function getLogPath(callback){
                    log_path = $LogService.GetLogPath();
                    if(callback)
                        callback();
                }

                //取得Log檔案清單
                function getLogList(callback){
                    $LogService.GetLogList(function(files){
                        total_size = 0;
                        recordList = [];
                        if(files && files.length > 0){
                            files.forEach(function(file){
                                var file_size = Number(file.state.size/1024).toFixed(1);
                                total_size = $calculater.decimalAdd(total_size, file_size);
                                recordList.push({file_name: file.fileName, file_size: file_size});
                            });
                        }
                        if(callback)
                            callback();
                    });
                }

                function openDialog(){
                    $mdDialog.dialog(
                        'module/system/program/logging/logging.html',
                        function(dialog){
                            return {
                                css_url:'module/system/program/logging/logging.css',
                                log_level: config.log.level || '',
                                log_path: log_path, //日誌位置
                                total_size: total_size, // 已用大小
                                days: config.log.days || 0, //保存天數
                                recordList: recordList, //日誌檔案清單
                                mode: 'list',
                                beforeShown: function(){
                                },
                                loadLogLevel: function(){
                                    //日誌等級下拉選單
                                    var typeList = [
                                        {code : '0', name: 'error'},
                                        {code : '1', name: 'warn'},
                                        {code : '2', name: 'info'},
                                        {code : '3', name: 'debug'},
                                        {code : '4', name: 'none'},
                                    ];
                                    $rootScope.showSelect({
                                        title : $filter('translate')('common.msg.plz_select'),
                                        label : 'name',
                                        code : 'code',
                                        order : 'code',
                                        selectCode : dialog.log_level,
                                        list : typeList,
                                        confirm : function(selectItem, TypeDialog){
                                            TypeDialog.hide();
                                            dialog.log_level = selectItem.name;

                                            //回寫Log層級
                                            if(selectItem.code == '4'){
                                                $LogService.ToFile(false);
                                            }else{
                                                $LogService.ToFile(selectItem.name);
                                            }
                                        }
                                    });
                                },
                                changeSaveDays: function(event){
                                    //修改保存天數
                                    var target = event.currentTarget;
                                    angular.element(target).addClass('is-focus');
                                    $calculater.open(function (num, calDialog) {
                                        calDialog.hide();
                                        dialog.days = +num;
                                        angular.element(target).removeClass('is-focus');
                                    },
                                    function (calDialog) {
                                        angular.element(target).removeClass('is-focus');
                                        calDialog.hide();
                                    },
                                    {
                                        defaultNum: dialog.days,
                                        decimal: true
                                    });
                                },
                                triggerSettingPage: function(){
                                    if('setting' == dialog.mode){
                                        dialog.mode = 'list';
                                    } else {
                                        dialog.mode = 'setting';
                                    }
                                },
                                saveSetting: function(){
                                    //儲存設定
                                    config.log.days = dialog.days;
                                    config.log.level = dialog.log_level;
                                    $LogService.SaveConfig(function(){
                                        $rootScope.showToast($filter('translate')('logging.msg.save_success'));
                                        dialog.goToListPage();
                                    });
                                },
                                deleteAllLog: function(){
                                    //清除日誌
                                    $rootScope.showConfirm($filter('translate')('logging.msg.is_delete_all_log'), function(confirmDialog){
                                        confirmDialog.hide();
                                        $LogService.DeleteLogFiles({isAll:true, callback:function(){
                                            $rootScope.showToast($filter('translate')('logging.msg.log_all_deleted'));
                                            dialog.refresh();
                                        }});
                                    });
                                },
                                openLogPath: function(){
                                    //開啟日誌路徑資料夾
                                    if(log_path && log_path != ''){
                                        $LogService.OpenLogPath(dialog.log_path);
                                    }
                                },
                                openLog: function(item){
                                    //開啟日誌檔案
                                    if(item.file_name && item.file_name != ''){
                                        $LogService.OpenLogFile(item.file_name);
                                    }
                                },
                                deleteLog: function(item){
                                    //刪除指定日誌
                                    $rootScope.showConfirm($filter('translate')('logging.msg.is_delete_log',{'file_name': item.file_name}), function(confirmDialog){
                                        confirmDialog.hide();
                                        $LogService.DeleteLogFile(item.file_name, function(){
                                            $rootScope.showToast($filter('translate')('logging.msg.log_deleted', {'file_name': item.file_name}));
                                            dialog.refresh();
                                        });
                                    });
                                },
                                refresh: function(){
                                    //更新明細頁面
                                    getLogInformation(function(){
                                        dialog.total_size = total_size;
                                        dialog.recordList = recordList;
                                    });
                                },
                                back: function () {
                                    dialog.hide();
                                },
                                confirm: function () {
                                    dialog.hide();
                                    if(options.feedback)
                                        options.feedback();
                                }
                            };
                        }
                    );
                }
                deviceCheck(function(){
                    getLogInformation(function(){
                        openDialog();
                    });
                });
            };
        }]);
});
